package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.SetmealDish;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.service.SetmealService;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/setmeal")
@Slf4j
public class SetmealController {

    @Autowired
    private SetmealService setmealService;


    // 新增套餐
    @PostMapping
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result insertSetmeal(@RequestBody SetmealDTO setmealDTO){
        log.info("新增套餐,接收到的参数是{}",setmealDTO);
        setmealService.insertSetmeal(setmealDTO);
        return Result.success();
    }

    // 套餐分页查询
    @GetMapping("/page")
    public Result pageQuery(SetmealPageQueryDTO dto){
        log.info("套餐分页查询,接收到的参数是{}",dto);
        PageResult pageResult = setmealService.pageQuery(dto);
        return Result.success(pageResult);
    }

    // 删除套餐
    @DeleteMapping
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result deleteByIds(@RequestParam List<Long> ids){
        log.info("删除套餐,接收到的参数是{}",ids);
        Integer i = setmealService.deleteByIds(ids);
        if(i == 1){
            return Result.success();
        }
        return Result.error("启售中的套餐不能删除");
    }

    // 根据id查询套餐
    @GetMapping("/{id}")
    public Result selectById(@PathVariable Long id){
        log.info("根据id查询套餐,接收到的参数是{}",id);
        SetmealVO setmealVO = setmealService.selectById(id);
        return Result.success(setmealVO);
    }

    // 修改套餐
    @PutMapping
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result update(@RequestBody SetmealDTO dto){
        log.info("修改套餐,接收到的参数是{}",dto);
        setmealService.update(dto);
        return Result.success();
    }

    // 启售停售套餐
    @PostMapping("/status/{status}")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    @Transactional
    public Result updateByStatus(@PathVariable Integer status,@RequestParam Long id){
        log.info("启售停售套餐,接收到的参数是{},{}",status,id);
        Integer i = setmealService.updateByStatus(status,id);
        if(i == 1){
            return Result.success();
        }
        return Result.error("该套餐中存在状态为停售的菜品，不能启售");
    }

}
